;AVC Wizard Installer Script
;Written by Pierre Biermann (template created by Joost Verburg)

;--------------------------------
;Include Modern UI

  !include "MUI2.nsh"

;--------------------------------
;General

  ;Name and file
  Name "AVC Wizard Setup (32 bit)"
  OutFile "AVCWizardX86Setup.exe"
  
  ;additional variables
  !define Publisher "PBSoft"
  !define ShortName "AVCWizard"
  !define RegKey "Software\${ShortName}"
  !define SourceDir "AVCWizard\bin\Release\"
  !define FFLibSrc "C:\ffmpeg\x86"

  ;Default installation folder
  InstallDir "$PROGRAMFILES\${Publisher}\${ShortName}"
  
  ;Get installation folder from registry if available
  InstallDirRegKey HKCU "${RegKey}" ""

  ;Request application privileges for Windows Vista
  RequestExecutionLevel admin

;--------------------------------
;Interface Settings

  !define MUI_ABORTWARNING

;--------------------------------
;Language Selection Dialog Settings

  ;Remember the installer language
  !define MUI_LANGDLL_REGISTRY_ROOT "HKCU" 
  !define MUI_LANGDLL_REGISTRY_KEY "${RegKey}" 
  !define MUI_LANGDLL_REGISTRY_VALUENAME "Installer Language"

;--------------------------------
;Pages

  !insertmacro MUI_PAGE_LICENSE "License.txt"
  !insertmacro MUI_PAGE_COMPONENTS
  !insertmacro MUI_PAGE_DIRECTORY
  !insertmacro MUI_PAGE_INSTFILES
  
  !insertmacro MUI_UNPAGE_CONFIRM
  !insertmacro MUI_UNPAGE_INSTFILES

;--------------------------------
;Languages

  !insertmacro MUI_LANGUAGE "English" ;first language is the default language
  !insertmacro MUI_LANGUAGE "German"

;--------------------------------
;Reserve Files
  
  ;If you are using solid compression, files that are required before
  ;the actual installation should be stored first in the data block,
  ;because this will make your installer start faster.
  
  !insertmacro MUI_RESERVEFILE_LANGDLL

;--------------------------------
;Installer Sections

Section "Program data" SecMain

  SetOutPath "$INSTDIR"
  
  ;ADD YOUR OWN FILES HERE...
  File "license.txt"
  File "${SourceDir}\AVCWizard.exe"
  File "${SourceDir}\FFShellAPI.dll"
  File "${SourceDir}\languages.xml"
  File "${SourceDir}\presets.xml"
  File "${SourceDir}\languages.xsd"
  File "${SourceDir}\presets.xsd"
  
  ;Store installation folder
  WriteRegStr HKCU "${RegKey}" "" $INSTDIR
  
  ;Create uninstaller
  WriteUninstaller "$INSTDIR\Uninstall.exe"
  
  ; Uninstaller regkeys
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${ShortName}" "DisplayName" "AVCWizard Audio Video Converter"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${ShortName}" "UninstallString" '"$INSTDIR\uninstall.exe"'
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${ShortName}" "Publisher" "${Publisher}"
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${ShortName}" "NoModify" 1
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${ShortName}" "NoRepair" 1
  
  ; Write language to ini file
  StrCmp $LANGUAGE ${LANG_ENGLISH} 0 +3
		WriteINIStr $INSTDIR\default.ini settings LanguageId en-US
		WriteINIStr $INSTDIR\default.ini settings SelectedLangId 0
  StrCmp $LANGUAGE ${LANG_GERMAN} 0 +3
		WriteINIStr $INSTDIR\default.ini settings LanguageId de-DE
		WriteINIStr $INSTDIR\default.ini settings SelectedLangId 1

SectionEnd

Section "FFmpeg encoder library" SecFFMpegLib

  SetOutPath "$INSTDIR\FFmpeg"
  
  File ${FFLibSrc}\avcodec-54.dll
  File ${FFLibSrc}\avdevice-54.dll
  File ${FFLibSrc}\avfilter-3.dll
  File ${FFLibSrc}\avformat-54.dll
  File ${FFLibSrc}\avutil-52.dll
  File ${FFLibSrc}\ffmpeg.exe
  File ${FFLibSrc}\ffplay.exe
  File ${FFLibSrc}\ffprobe.exe
  File ${FFLibSrc}\postproc-52.dll
  File ${FFLibSrc}\swresample-0.dll
  File ${FFLibSrc}\swscale-2.dll
  File ${FFLibSrc}\readme.txt
  
  ; Write FFmpeg path to ini
  WriteINIStr $INSTDIR\default.ini settings FFMpegPath "$INSTDIR\FFmpeg"
  
SectionEnd

; Optional section (can be disabled by the user)
Section "Start menu shortcuts" ShortCuts

  CreateDirectory "$SMPROGRAMS\${ShortName}"
  CreateShortCut "$SMPROGRAMS\${ShortName}\${ShortName}.lnk" "$INSTDIR\${ShortName}.exe" "" "$INSTDIR\${ShortName}.exe" 0
  CreateShortCut "$SMPROGRAMS\${ShortName}\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0

SectionEnd

Section "Desktop shortcut" DeskShortCut

  CreateShortCut "$DESKTOP\${ShortName}.lnk" "$INSTDIR\${ShortName}.exe" "" "$INSTDIR\${ShortName}.exe" 0

SectionEnd

;--------------------------------
;Installer Functions

Function .onInit

  SetRegView 32
  !insertmacro MUI_LANGDLL_DISPLAY

FunctionEnd

;--------------------------------
;Descriptions

  ;USE A LANGUAGE STRING IF YOU WANT YOUR DESCRIPTIONS TO BE LANGAUGE SPECIFIC

  ;Assign descriptions to sections
  !insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
    !insertmacro MUI_DESCRIPTION_TEXT ${SecMain} "Main section"
	!insertmacro MUI_DESCRIPTION_TEXT ${SecFFMpegLib} "FFmpeg encoder library"
	!insertmacro MUI_DESCRIPTION_TEXT ${ShortCuts} "Start Menu Shortcuts"
	!insertmacro MUI_DESCRIPTION_TEXT ${DeskShortCut} "Desktop Shortcut"
  !insertmacro MUI_FUNCTION_DESCRIPTION_END

 
;--------------------------------
;Uninstaller Section

Section "Uninstall"

  ;ADD YOUR OWN FILES HERE...
  
  ; Remove ffmpeg library
  Delete "$INSTDIR\FFmpeg\avcodec-54.dll"
  Delete "$INSTDIR\FFmpeg\avdevice-54.dll"
  Delete "$INSTDIR\FFmpeg\avfilter-3.dll"
  Delete "$INSTDIR\FFmpeg\avformat-54.dll"
  Delete "$INSTDIR\FFmpeg\avutil-52.dll"
  Delete "$INSTDIR\FFmpeg\ffmpeg.exe"
  Delete "$INSTDIR\FFmpeg\ffplay.exe"
  Delete "$INSTDIR\FFmpeg\ffprobe.exe"
  Delete "$INSTDIR\FFmpeg\postproc-52.dll"
  Delete "$INSTDIR\FFmpeg\swresample-0.dll"
  Delete "$INSTDIR\FFmpeg\swscale-2.dll"
  Delete "$INSTDIR\FFmpeg\readme.txt"
  
  ; Remove main program data
  Delete "$INSTDIR\Uninstall.exe"
  Delete "$INSTDIR\license.txt"
  Delete "$INSTDIR\AVCWizard.exe"
  Delete "$INSTDIR\FFShellAPI.dll"
  Delete "$INSTDIR\languages.xml"
  Delete "$INSTDIR\presets.xml"
  Delete "$INSTDIR\languages.xsd"
  Delete "$INSTDIR\presets.xsd"
  Delete "$INSTDIR\default.ini"

  RMDir "$INSTDIR\FFmpeg"
  RMDir "$INSTDIR"
  
  ; Remove shortcuts, if any
  Delete "$DESKTOP\${ShortName}.lnk"
  RMDir /r "$SMPROGRAMS\${ShortName}"

  ; Delete reg keys
  DeleteRegKey HKCU "${RegKey}"
  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${ShortName}"

SectionEnd

;--------------------------------
;Uninstaller Functions

Function un.onInit

  !insertmacro MUI_UNGETLANGUAGE
  
FunctionEnd